//
//  ViewController.m
//  LKDBTest
//
//  Created by 江湖 on 2018/6/25.
//  Copyright © 2018年 江湖. All rights reserved.
//

#import "ViewController.h"
#import "LKDBModel.h"
#import "RowCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic , strong) NSMutableArray *dataArray;
@property (weak, nonatomic) IBOutlet UITextField *ageTextfile;
@property (weak, nonatomic) IBOutlet UITextField *sexTextfile;
@property (weak, nonatomic) IBOutlet UITextField *nameTextfile;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",[self downloadPath]);
    [self upData];
//    [LKDBModel insterData];
//    [LKDBModel insterData];

    // Do any additional setup after loading the view, typically from a nib.
}
-(void)upData{
    [self.dataArray removeAllObjects];
    [self.dataArray addObjectsFromArray:[LKDBModel getData]];
    [self.tableView reloadData];
}
- (IBAction)inserterData:(id)sender {
//    for (int i=0; i<12; i++) {
        LKDBModel *model = [[LKDBModel alloc]init];

        model.name = self.nameTextfile.text;
        model.age = self.ageTextfile.text;
        model.sex = self.sexTextfile.text;
        model.dic = @{@"car":@"宝马"};
        [LKDBModel insterData:model];
//    }

    [self upData];
    
   NSArray *arr = [LKDBModel getData];
    for (LKDBModel *model in arr) {
        NSLog(@"%@",model.dic);

    }
}
- (IBAction)deleteData:(id)sender {
    [LKDBModel deleteData];
    [LKDBModel deleteAllTable];
    [self upData];

}
- (IBAction)editData:(id)sender {
    [LKDBModel updata];
    [self upData];

}
- (IBAction)deleteModel:(id)sender {
    [LKDBModel deleteModel];
    [self upData];

}

- (NSString *)downloadPath{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *downloadPath = [documentPath stringByAppendingPathComponent:@"wjy"];
    NSLog(@"%@",downloadPath);
    return downloadPath;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LKDBModel *model = self.dataArray[indexPath.row];
    RowCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = model;
    
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LKDBModel *model = self.dataArray[indexPath.row];
    [LKDBModel deleteObj:model];
    [self upData];
    
}
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
